<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>LOCALIDADES!!!</title>
<script language="JavaScript" type="text/javascript" src="javascript/jquery/jquery-1.3.2.min.js"></script>
</head>

<body>
<script language="JavaScript" type="text/javascript">
var array = new Array();

array[1] = {"pais": "CAPITAL FEDERAL","pais": "BALVANERA","localidad": "","url": "http://maps.google.com/maps/geo?q=BALVANERA,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[2] = {"pais": "CAPITAL FEDERAL","pais": "BELGRANO","localidad": "","url": "http://maps.google.com/maps/geo?q=BELGRANO,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[3] = {"pais": "CAPITAL FEDERAL","pais": "CONCEPCION","localidad": "","url": "http://maps.google.com/maps/geo?q=CONCEPCION,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[4] = {"pais": "CAPITAL FEDERAL","pais": "CRISTO OBRERO","localidad": "","url": "http://maps.google.com/maps/geo?q=CRISTO+OBRERO,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[5] = {"pais": "CAPITAL FEDERAL","pais": "FLORES","localidad": "","url": "http://maps.google.com/maps/geo?q=FLORES,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[6] = {"pais": "CAPITAL FEDERAL","pais": "LAS HERAS","localidad": "","url": "http://maps.google.com/maps/geo?q=LAS+HERAS,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[7] = {"pais": "CAPITAL FEDERAL","pais": "MONSERRAT","localidad": "","url": "http://maps.google.com/maps/geo?q=MONSERRAT,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[8] = {"pais": "CAPITAL FEDERAL","pais": "NUESTRA SEÑORA DEL CARMEN","localidad": "","url": "http://maps.google.com/maps/geo?q=NUESTRA+SEÑORA+DEL+CARMEN,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[9] = {"pais": "CAPITAL FEDERAL","pais": "PALERMO","localidad": "","url": "http://maps.google.com/maps/geo?q=PALERMO,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[10] = {"pais": "CAPITAL FEDERAL","pais": "PILAR","localidad": "","url": "http://maps.google.com/maps/geo?q=PILAR,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[11] = {"pais": "CAPITAL FEDERAL","pais": "SAAVEDRA","localidad": "","url": "http://maps.google.com/maps/geo?q=SAAVEDRA,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[12] = {"pais": "CAPITAL FEDERAL","pais": "SAN BERNARDO","localidad": "","url": "http://maps.google.com/maps/geo?q=SAN+BERNARDO,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[13] = {"pais": "CAPITAL FEDERAL","pais": "SAN CARLOS","localidad": "","url": "http://maps.google.com/maps/geo?q=SAN+CARLOS,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[14] = {"pais": "CAPITAL FEDERAL","pais": "SAN CRISTOBAL","localidad": "","url": "http://maps.google.com/maps/geo?q=SAN+CRISTOBAL,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[15] = {"pais": "CAPITAL FEDERAL","pais": "SAN JOSE","localidad": "","url": "http://maps.google.com/maps/geo?q=SAN+JOSE,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[16] = {"pais": "CAPITAL FEDERAL","pais": "SAN JUAN EVANGELISTA","localidad": "","url": "http://maps.google.com/maps/geo?q=SAN+JUAN+EVANGELISTA,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[17] = {"pais": "CAPITAL FEDERAL","pais": "SAN LUIS GONZAGA","localidad": "","url": "http://maps.google.com/maps/geo?q=SAN+LUIS+GONZAGA,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[18] = {"pais": "CAPITAL FEDERAL","pais": "SAN NICOLAS","localidad": "","url": "http://maps.google.com/maps/geo?q=SAN+NICOLAS,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[19] = {"pais": "CAPITAL FEDERAL","pais": "SAN VICENTE DE PAUL","localidad": "","url": "http://maps.google.com/maps/geo?q=SAN+VICENTE+DE+PAUL,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[20] = {"pais": "CAPITAL FEDERAL","pais": "SANTA LUCIA","localidad": "","url": "http://maps.google.com/maps/geo?q=SANTA+LUCIA,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[21] = {"pais": "CAPITAL FEDERAL","pais": "SOCORRO","localidad": "","url": "http://maps.google.com/maps/geo?q=SOCORRO,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[22] = {"pais": "CAPITAL FEDERAL","pais": "VELEZ SARSFIELD","localidad": "","url": "http://maps.google.com/maps/geo?q=VELEZ+SARSFIELD,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[23] = {"pais": "CAPITAL FEDERAL","pais": "VERSALLES","localidad": "","url": "http://maps.google.com/maps/geo?q=VERSALLES,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};
array[24] = {"pais": "CAPITAL FEDERAL","pais": "VILLA LUGANO","localidad": "","url": "http://maps.google.com/maps/geo?q=VILLA+LUGANO,+CAPITAL+FEDERAL,+ARGENTINA&output=json&key=ABQIAAAAI2EbZ-IX5h9qJnbCXSo4IxQHs-HuxRNJK5szmmE85b_JabALFRShj5Qvj3j1M4BwQpDp5n_g5eyitw"};



$(document).ready(mostar);
var i = 1;
var fin = array.length;
function mostar(){	
	window.setTimeout(function(){		
		if (i<fin){
			window.status = "Prosesando "+ i + "/ " + array.length;
			buscar(array[i]);
			mostar();
			i++;
		}			
	},500);
	
}

function buscar(elem) {
	$.ajax({
		dataType: "jsonp",
		data: "",
		url: elem.url + "&callback=?",
		success: function(json,txtsstatus){ jasonpCallback(json,txtsstatus); }
	});
}

function jasonpCallback(data, textStatus) {	
  	// data will be a jsonObj
  	// textStatus will be one of the following values: "timeout","error","notmodified","success","parsererror"
	  //this; // the options for this ajax request
	  //alert("!" + elem);
	  var pais, provincia, depto, loc;
	  
	  var chunks = data.name.split(",");
	  if (chunks.length == 4){
	  	pais = chunks[3];
	  	provincia = chunks[2];
		depto = chunks[1];
		loc = chunks[0];
	  } else {
	  	pais = chunks[2];
	  	provincia = chunks[1];
		depto = chunks[0];
		loc = chunks[0];	  
	  }
	  var row = "<TR>";
	  row += "<td>" + data.name  + "</TD>";
	  
	   row += "<td>" + pais  + "</TD>";
	   row += "<td>" + provincia  + "</TD>";
	   row += "<td>" + depto  + "</TD>";
	   row += "<td>" + loc + "</TD>";
	  
	  if (data.Status.code == 200){
	  	  var acc = data.Placemark[0].AddressDetails.Accuracy;
		  var pm =  data.Placemark[0]; 
		  
		  if (data.Placemark.length > 1) {		  
		  	var aux = 0;
			var mensaje = "Buscado: " + data.name + "\n\nPosibilidades:\n\n";
		  	for (aux=0; aux < data.Placemark.length ; aux++){
				mensaje += aux + ":" + data.Placemark[aux].address + " (" + data.Placemark[aux].Point.coordinates[0] +" - " + data.Placemark[aux].Point.coordinates[1] + ")\n";
			}
			mensaje += "\nCANCELAR para dejar en blanco";
			var idx = prompt(mensaje, 0);
			pm =  data.Placemark[idx]; 

		  }
		  //var acc = data.Placemark[data.Placemark.length-1].AddressDetails.Accuracy;
		  //var pm =  data.Placemark[data.Placemark.length-1]; 

		  var aux = "";
		  try {		  
			  aux = acc >= 1 ? pm.AddressDetails.Country.CountryName : "";
			  row += "<td>" + aux + "</TD>";
			  aux = (acc >= 2 && pm.AddressDetails.Country.AdministrativeArea != null) ? pm.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName : "";
			  row += "<td>" + aux + "</TD>";
			 
			  aux = buscaCiudad(pm);
			  
			  row += "<td>" + aux + "</TD>";
			  row += "<td>" + (""+pm.Point.coordinates[0]).replace("\.",",") + "</TD>";
			  row += "<td>" + (""+pm.Point.coordinates[1]).replace("\.",",") + "</TD>";	  
			  //row += "<td>" + (""+pm.Point.coordinates[0]+ "</TD>";
			  //row += "<td>" + pm.Point.coordinates[1]+ "</TD>";	  
			  
			  row += "<td>" + pm.address + "</TD>";	  
		  } catch (e) {
		  
		  	//alert(e);
			var j = 33;
			j++;
		  }
	  } else {
			row += "<td> </TD>";
			row += "<td> </TD>";
			row += "<td> </TD>";
			row += "<td> </TD>";
			row += "<td> </TD>";	  
			row += "<td> </TD>";	 	  	
	  }
	  row += "</TR>";
	  $("#tabla").append(row);
	}
	
	/**
	Recive un PlaceMark
	*/
	function buscaCiudad(pm) {
		var aux = buscaLocEnSubAdmin(pm);
		
		if (aux == null)
			aux  = busca_Country_AddressLine(pm);
		return aux;
	}
	
	function buscaLocEnSubAdmin(pm){
		if (pm.AddressDetails.Country.AdministrativeArea != null)
			if (pm.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea != null){			
				if (pm.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality != null ) {
					return buscaEnLocality(pm.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality);
				} else if (pm.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName != null ) {
					return pm.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;
				}
			} else if (pm.AddressDetails.Country.AdministrativeArea.Locality != null){
				return buscaEnLocality(pm.AddressDetails.Country.AdministrativeArea.Locality);
			}
						
	 return null;	
	}
	
	/**
	Busca la ciudad a partir de un objeto Locality
	*/
	function buscaEnLocality(loc){
		if (loc.AddressLine != null)
			return loc.AddressLine[0];
		else 
			return loc.LocalityName;	
	}
	
	function busca_Country_AddressLine(pm){
		if (pm.AddressDetails.Country.AddressLine != null){
			return pm.AddressDetails.Country.AddressLine;
		}
		return null;
	}
	
</script>

<table id="tabla" border="1">
<tr>
	<td>completo</td>
	
	<td>pais</td>
	<td>provincia</td>
	<td>depto_loc</td>
	<td>localidad</td>
	
	<td>pais</td>
	<td>provincia</td>
	<td>localidad</td>
	<td>latitud</td>
	<td>longitud</td>
	<td>Google Address</td>
</tr>
</table>
</body>

</html>
